<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Lib\Think\Page;
use Common\Controller\AdminBaseController;

class CommentController extends AdminBaseController
{
    protected $comment_model;

    public function _initialize() {
        parent::_initialize();
        $this->comment_model = D('Common/Comment');
    }

    public  function index() {
        $where = array();

        //审核状态
        if ($status = I('post.status','0','intval')) {
            $where['status'] = ($status === 1 ? 1 : 0);
            $this->assign('status',$status);
        }

        $count = $this->comment_model->where($where)->count();
        // 实例化分页类 传入总记录数和每页显示的记录数
        $page = new Page($count, 16);
        // 分页显示输出
        $show = $page->show();

        $list = $this->comment_model->where($where)->order(array('comment_id'=>'DESC'))->limit($page->firstRow . ',' . $page->listRows)->select();

        $articleIds = array();
        foreach($list as $k => $v){
            $articleIds[$v['article_id']] = $v['article_id'];
        }

        if(!empty($articleIds)){
            $this->assign('articles', D('Common/Article')->itemsByIds($articleIds));
        }

        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    // 通过或者取消审核
    public function changeStatus($comment_id = 0)
    {
        $comment_id = I('post.comment_id',0,'intval');

        if (is_numeric($comment_id)) {
            if (!$detail = $this->comment_model->find($comment_id)) {
                $this->error('请选择要改变审核状态的评论');
            }
            $data['status'] = $detail['status'] ? 0 : 1 ;
            $data['comment_id'] = $comment_id;
            if ($this->comment_model->save($data) !== false) {
                $this->success('审核状态改变成功',U('index'));
            } else {
                $this->error('审核状态改变失败');
            }
        } elseif (is_array($comment_id)) {
            foreach($comment_id as $id){
                $detail = $this->comment_model->find($id);
                if($detail['status'] == 0) {
                    $this->comment_model->save(array('comment_id'=>$id, 'status'=>1));
                }
            }
            $this->success('批量审核成功', U('index'));
        } else {
            $this->error('请选择要改变审核状态的评论');
        }
    }

    public function delete() {

        $comment_id = I('post.comment_id',0,'intval');

        if (is_numeric($comment_id)) {
            $commentList = $this->comment_model->where(array('parent_id'=>$comment_id))->find();;
            if ($commentList) {
                $this->error('该评论下面包含回复评论,请先删除回复评论');
            }
            $this->comment_model->delete($comment_id);
            $this->success('删除成功！', U('index'));
        } elseif (is_array($comment_id)) {
            $this->comment_model->delete(implode(',', $comment_id));
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的评论');
        }
    }
}
